package com.oocl.message.handler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.oocl.chatroom.network.protocol.Message;
import com.oocl.dao.UserDao;
import com.oocl.dao.impl.UserDaoImpl;
import com.oocl.message.sender.MsgSender;
import com.oocl.service.UserService;
import com.oocl.service.impl.UserServiceImpl;

public class MsgHandler {
	private Socket socket;
	private PrintWriter out;
	private MsgSender ms;
	private Message msg;
	private UserService uservice;

	public MsgHandler(Socket socket) {
		super();
		this.socket = socket;
		uservice=new UserServiceImpl();
		try {
			out=new PrintWriter(socket.getOutputStream());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		ms=new MsgSender(out);
		uservice.setUserDao(new UserDaoImpl());
		try {
			this.out=new PrintWriter(socket.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void lgNotify(String name,String pwd){
		if(uservice.login(name, pwd)){
			msg=new Message("",8,"","",System.currentTimeMillis());
			try {
				ms.send(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			msg=new Message("",7,"","",System.currentTimeMillis());
			try {
				ms.send(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void regNotify(String name,String pwd){
		ms=new MsgSender(out);
		//System.out.println(uservice);
		if(uservice.register(name, pwd)){
			msg=new Message("",8,"","",System.currentTimeMillis());
			try {
				ms.send(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			msg=new Message("",7,"","",System.currentTimeMillis());
			try {
				ms.send(msg);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
